/*
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
 *
 * Copyright (c) 2015 - 2025 CCBlueX
 *
 * LiquidBounce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LiquidBounce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
 */
package net.ccbluex.liquidbounce.features.itemgroup.groups

import com.mojang.authlib.properties.Property
import com.mojang.authlib.properties.PropertyMap
import net.ccbluex.liquidbounce.api.core.HttpClient
import net.ccbluex.liquidbounce.api.core.HttpMethod
import net.ccbluex.liquidbounce.api.core.ioScope
import net.ccbluex.liquidbounce.api.core.parse
import net.ccbluex.liquidbounce.api.core.retrying
import net.ccbluex.liquidbounce.features.itemgroup.ClientItemGroup
import net.ccbluex.liquidbounce.render.engine.type.Color4b
import net.ccbluex.liquidbounce.utils.client.asPlainText
import net.ccbluex.liquidbounce.utils.client.logger
import net.minecraft.component.ComponentChanges
import net.minecraft.component.DataComponentTypes
import net.minecraft.component.type.LoreComponent
import net.minecraft.component.type.ProfileComponent
import net.minecraft.item.ItemStack
import net.minecraft.item.Items
import net.minecraft.registry.Registries
import net.minecraft.text.Style
import net.minecraft.util.Formatting
import java.util.*
import kotlin.time.Duration.Companion.seconds

data class Head(val name: String, val uuid: UUID, val value: String) {

    fun asItemStack(): ItemStack {
        val builder = ComponentChanges.builder()

        builder.add(DataComponentTypes.CUSTOM_NAME, name.asPlainText(CUSTOM_NAME_STYLE))
        builder.add(
            DataComponentTypes.LORE,
            LoreComponent(
                listOf(
                    "UUID: $uuid".asPlainText(UUID_STYLE),
                    "liquidbounce.net".asPlainText(CLIENT_LINK_STYLE),
                )
            )
        )
        builder.add(
            DataComponentTypes.PROFILE,
            ProfileComponent(
                Optional.empty(),
                Optional.empty(),
                PropertyMap().apply {
                    put("textures", Property("textures", value))
                }
            )
        )


        return ItemStack(Registries.ITEM.getEntry(Items.PLAYER_HEAD), 1, builder.build())
    }


    companion object {
        @JvmStatic
        private val CUSTOM_NAME_STYLE = Style.EMPTY.withFormatting(Formatting.GOLD, Formatting.BOLD)

        @JvmStatic
        private val UUID_STYLE = Style.EMPTY.withFormatting(Formatting.GRAY)

        @JvmStatic
        private val CLIENT_LINK_STYLE = Style.EMPTY.withColor(Color4b.LIQUID_BOUNCE.toARGB())
    }
}

class HeadsItemGroup : ClientItemGroup(
    "Heads",
    icon = { Items.SKELETON_SKULL.defaultStack },
    items = { items ->
        heads.getNow()?.let { heads ->
            items.addAll(heads.distinctBy { it.name }.map(Head::asItemStack))
        }
    }
) {
    companion object {
        /**
         * The API endpoint to fetch heads from which is owned by CCBlueX
         * and therefore can reliably depend on.
         */
        const val HEAD_DB_API = "https://headdb.org/api/category/all"

        val heads = ioScope.retrying(
            interval = 1.seconds,
            name = "player-heads",
            maxRetries = 3,
        ) {
            val heads: HashMap<String, Head> = HttpClient.request(HEAD_DB_API, HttpMethod.GET).parse()

            heads.values.also {
                logger.info("Successfully loaded ${it.size} heads from HeadDB")
            } as Collection<Head>
        }
    }
}
